import rx.Observable;

import java.util.ArrayList;

/**
 * Created by codeczx on 2017/2/16.
 */
public class AggregationTest {

    void testReduce(){
        Observable<Integer> values = Observable.range(0,5);
        values.reduce((i1,i2)->i1+i2)
                .subscribe(new PrintSubscriber("sum"));
        values.reduce((i1,i2)->(i1>i2)?i2:i1)
                .subscribe(new PrintSubscriber("min"));
    }

    void testReduceAsList(){
        Observable<Integer> values = Observable.range(0,5);
        values.reduce(new ArrayList<Integer>(),(aac,next)->{
            aac.add(next);
            return aac;
        }).subscribe(System.out::println);
    }

    void testScan(){
        Observable<Integer> values = Observable.range(0,5);
        values.scan(0,(i1,i2)->i1+i2)
                .takeLast(1)
                .subscribe(new PrintSubscriber("scan"));
    }

    void testScan2(){
        Observable.concat(
                Observable.range(0,3),
                Observable.range(0,3),
                Observable.range(0,3)
        )
                .scan(0,(v,acc)->v+1)
                .subscribe(System.out::println);
    }

    void testToSortedList(){
        Observable<Integer> values = Observable.just(15,14,18,17);
        values.toSortedList((i1,i2)->i2-i1)
                .subscribe(System.out::println);
    }

    public static void main(String[] args) {
        AggregationTest cdt = new AggregationTest();
//        cdt.testReduce();
//        cdt.testScan();
//        cdt.testReduceAsList();
//        cdt.testToSortedList();
        cdt.testScan2();
    }

}
